#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("QDesktop");
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{

    ui->lcdNumber->setStyleSheet("QLCDNumber {background: black}");
    ui->lcdNumber_2->setStyleSheet("QLCDNumber {background: black}");
    ui->lcdNumber_3->setStyleSheet("QLCDNumber {background: black}");
    ui->lcdNumber_4->setStyleSheet("QLCDNumber {background: black}");

//    ui->lcdNumber_5->setFrameStyle(QFrame::Sunken);
    ui->lcdNumber_5->setStyleSheet("QLCDNumber {background: black}");
    ui->lcdNumber_6->setStyleSheet("QLCDNumber {background: black}");


    m_timer = new QTimer;
    m_timer->setInterval(500);
    connect(m_timer, &QTimer::timeout, this, &MainWindow::timeout);
    m_timer->start();
}

void MainWindow::timeout()
{
    QDateTime dateTime = QDateTime::currentDateTime();

    QDate date = dateTime.date();
    QTime time = dateTime.time();

    ui->lcdNumber_4->display(date.year());
    ui->lcdNumber_5->display(date.month());
    ui->lcdNumber_6->display(date.day());

    ui->lcdNumber->display(time.hour());
    ui->lcdNumber_2->display(time.minute());
    ui->lcdNumber_3->display(time.second());

    static int diskCounter = 20;
    if(diskCounter >= 0){
        diskCounter++;
        if(diskCounter >= 10){//10s
            diskCounter = 0;
            QStringList list = disk();
            displayDisk(list);
        }
    }
}

QStringList MainWindow::disk()
{
    QStringList m_diskDescribe;
    QFileInfoList list = QDir::drives();
    foreach (QFileInfo dir, list)
    {
        QString dirName = dir.absolutePath();
        dirName.remove("/");
        LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16();
        ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes;
        if(GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes) )
        {
            QString free = QString::number((double) liTotalFreeBytes.QuadPart / GB, 'f', 1);
            free += "G";
            QString all = QString::number((double) liTotalBytes.QuadPart / GB, 'f', 1);
            all += "G";

            QString str = QString("%1%2/%3").arg(dirName, free, all);
            m_diskDescribe.append(str);
        }
    }

    return m_diskDescribe;
}

void MainWindow::displayDisk(QStringList list)
{
//    qDebug()<<__FUNCTION__<<m_processBarList.length()<<list.length();
    if(m_processBarList.length() != list.length()){
        foreach (SLP* lp, m_processBarList) {
            lp->label->deleteLater();
            lp->bar->deleteLater();
            lp->layout->deleteLater();
        }
        m_processBarList.clear();

        for(int i=0; i<list.length(); i++){
            QProgressBar* bar = new QProgressBar();
            QLabel* label = new QLabel();
            QHBoxLayout* layout = new QHBoxLayout();

            SLP* lp = new SLP;
            lp->bar = bar;
            lp->label = label;
            lp->layout = layout;

            m_processBarList.append(lp);

            bar->setRange(0, 100);

            layout->addWidget(label);
            layout->addWidget(bar);
            QVBoxLayout* v = (QVBoxLayout*)centralWidget()->layout();
            v->addLayout(layout);
        }
    }

    int a = 0;
    foreach (QString str, list) {
        QRegExp rx("(.):(.*)G/(.*)G");
        rx.indexIn(str, 0);

        float v = (rx.cap(3).toFloat()-rx.cap(2).toFloat())/rx.cap(3).toFloat();
        m_processBarList[a]->bar->setValue(v*100);
        m_processBarList[a]->label->setText(str);
        a++;
//        qDebug()<<str<<rx.cap(1)<<rx.cap(2)<<rx.cap(3)<<v;
    }
}
